/* 
*  Copyright (C) 2009-2010 WCG Project
*
*  This program is free software; you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by the
*  Free Software Foundation; either version 2 of the License, or (at
*  your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but
*  WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software Foundation,
*  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*  In addition, as a special exception, the author gives permission to
*  link the code of this program with the Half-Life Game Engine ("HL
*  Engine") and Modified Game Libraries ("MODs") developed by Valve,
*  L.L.C ("Valve"). You must obey the GNU General Public License in all
*  respects for all of the code used other than the HL Engine and MODs
*  from Valve. If you modify this file, you may extend this exception
*  to your version of the file, but you are not obligated to do so. If
*  you do not wish to do so, delete this exception statement from your
*  version.
*/



EFFECT_ScreenFade( id, Float:fadeTime=0.0, Float:fadeHold=0.0, rgba_color=0, fadeFlags=FFADE_IN )
{
	if( !is_user_connected( id ) || is_user_bot( id ) )
		return

	else if( PlayerInfo[id][PI_IsFlashed] )
		return

	new duration = FixedUnsigned16( fadeTime, (1<<12) )
	new holdtime = FixedUnsigned16( fadeHold, (1<<12) )

	message_begin( MSG_ONE, gmsgScreenFade, {0,0,0}, id )
	write_short( duration )
	write_short( holdtime )
	write_short( fadeFlags )
	write_byte( COLOR_RED(rgba_color) )
	write_byte( COLOR_GREEN(rgba_color) )
	write_byte( COLOR_BLUE(rgba_color) )
	write_byte( COLOR_ALPHA(rgba_color) )
	message_end()
}



EFFECT_ScreenShake( id, Float:fAmplitude, Float:fDuration, Float:fFrequency )
{
	if( !is_user_connected( id ) || is_user_bot( id ) )
		return

	new amplitude = FixedUnsigned16( fAmplitude, (1<<12) )
	new duration = FixedUnsigned16( fDuration, (1<<12) )
	new frequency = FixedUnsigned16( fFrequency, (1<<8) )

	message_begin( MSG_ONE, gmsgScreenShake, {0,0,0}, id )
	write_short( amplitude )
	write_short( duration )
	write_short( frequency )
	message_end()
}




EFFECT_Implosion( id=0, position[3], radius, count, life )
{
	if( id )
	{
		if( !is_user_alive( id ) )
			return

		message_begin( MSG_ONE, SVC_TEMPENTITY, _, id )
	}

	else
		message_begin( MSG_BROADCAST, SVC_TEMPENTITY )
	
		
	write_byte ( TE_IMPLOSION )
	write_coord( position[0] )			// position (X)
	write_coord( position[1] )			// position (Y)
	write_coord( position[2] )			// position (Z)
	write_byte ( radius )				// radius
	write_byte ( count )				// count
	write_byte ( life )					// life in 0.1's
	message_end()
}




EFFECT_EntityLight( entity, position[3] = {0,0,0}, radius = 100, rgb_color = 0xffffffff, Float:life = 1.0, decayrate = 0 )
{
	static vOrigin[3]

	if( !pev_valid( entity ) )
		return

	if( !position[0] && !position[1] && !position[2] )
	{
		new Float:fPos[3]
		pev( entity, pev_origin, fPos )
		vOrigin[0] = floatround( fPos[0] )
		vOrigin[1] = floatround( fPos[1] )
		vOrigin[2] = floatround( fPos[2] )
		vOrigin[2] += 75
	}
	else
	{
		vOrigin[0] = position[0]
		vOrigin[1] = position[1]
		vOrigin[2] = position[2]
	}

	message_begin( MSG_BROADCAST, SVC_TEMPENTITY )
	write_byte( TE_ELIGHT )
	write_short( entity )
	write_coord( vOrigin[0] )
	write_coord( vOrigin[1] )
	write_coord( vOrigin[2] )
	write_coord( radius )
	write_byte( COLOR_RED(rgb_color) )
	write_byte( COLOR_GREEN(rgb_color) )
	write_byte( COLOR_BLUE(rgb_color) )
	write_byte( floatround( life * 10.0 ) )
	write_coord( decayrate )
	message_end()
}




EFFECT_BeamSpiral( vOrigin[3], Radius, Height, Segments, NumSegments, iSprite, Float:fSeconds=1.0, BeamWidth=10, Noise=0, rgba_color=0xffffffff )
{
	static i, iCount, vStart[3], vEnd[3]
	static x1, y1, x2, y2, Float:theta1, Float:theta2, Float:fPointHeight, Float:fRadius

	fPointHeight = (float(Height) / float(NumSegments) ) / float(Segments)
	fRadius = float( Radius )
	iCount = Segments * NumSegments
	
	for( i = 0 ; i < iCount ; i++ )
	{
		theta1 = (i) * M_PI * 2 / Segments
		theta2 = (i+1) * M_PI * 2 / Segments

		x1 = floatround( floatcos( theta1 ) * fRadius )
		y1 = floatround( floatsin( theta1 ) * fRadius )
		x2 = floatround( floatcos( theta2 ) * fRadius )
		y2 = floatround( floatsin( theta2 ) * fRadius )

		vStart[0] = vOrigin[0] + x1
		vStart[1] = vOrigin[1] + y1
		vStart[2] = vOrigin[2] + floatround(i * fPointHeight)

		vEnd[0] = vOrigin[0] + x2
		vEnd[1] = vOrigin[1] + y2
		vEnd[2] = vOrigin[2] + floatround((i + 1) * fPointHeight)

		Create_TE_BEAMPOINTS( vStart, vEnd, iSprite, 0, 0, floatround( fSeconds * 10 ), BeamWidth, Noise, rgba_color )
	}
}
